package com.java.example.ui.dialog

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.java.example.R
import com.java.example.bean.VersionBean
import com.java.example.ui.update.UpdateService
import com.java.example.util.PermissionGroup

class UpdateUnForceDialog : View.OnClickListener {
    private lateinit var mDialog: AlertDialog
    private var mBean: VersionBean? = null
    private var mContext: Context? = null

    fun showDialog(context: Context?, bean: VersionBean?) {
        this.mContext = context
        this.mBean = bean
        if (::mDialog.isInitialized) {
            mDialog.show()
        } else {
            try {
                mDialog = AlertDialog.Builder(context, R.style.normal_dialog).create()
                mDialog.setCancelable(false)
                mDialog.setCanceledOnTouchOutside(false)
                mDialog.show()
                val window = mDialog.getWindow()
                if (window != null) {
                    window.setContentView(R.layout.dialog_version_unforce_update)
                    window.setGravity(Gravity.CENTER)
                    window.setWindowAnimations(R.style.anim_panel_up_from_bottom)
                    window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                    //设置属性
                    val params = window.attributes
                    params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
                    params.dimAmount = 0.5f
                    window.attributes = params
                    window.findViewById<View>(R.id.tv_update).setOnClickListener(this)
                    window.findViewById<View>(R.id.tv_cancel).setOnClickListener(this)
                    val tvCotent = window.findViewById<TextView>(R.id.tv_content)
                    tvCotent.text = mBean!!.content
                    val tvVersionName = window.findViewById<TextView>(R.id.tv_version_name)
                    tvVersionName.text = "V" + mBean!!.version_name
                }
            } catch (e: Exception) {
            }
        }
    }

    override fun onClick(v: View) {
        val id = v.id
        if (id == R.id.tv_cancel) {
            mDialog.dismiss()
        } else if (id == R.id.tv_update) {
            mDialog.dismiss()
            if (mBean == null) {
                return
            }
            if (PermissionUtils.isGranted(*PermissionGroup.permissionFile)) {
                val intent = Intent(mContext, UpdateService::class.java)
                intent.putExtra("apkUrl", mBean!!.apk_url)
                mContext!!.startService(intent)
            } else {
                PermissionUserDialog.create().showDialog(mContext, "为体验测量大师完整的版本升级功能，现申请开启本地存储相关权限") {
                    PermissionUtils.permission(*PermissionGroup.permissionFile).callback(object : PermissionUtils.SimpleCallback {
                        override fun onGranted() {
                            val intent = Intent(mContext, UpdateService::class.java)
                            intent.putExtra("apkUrl", mBean!!.apk_url)
                            mContext!!.startService(intent)
                        }

                        override fun onDenied() {
                            ToastUtils.showLong(R.string.no_limit)
                        }
                    }).request()
                }
            }
        }
    }

    companion object {
        fun create(): UpdateUnForceDialog {
            return UpdateUnForceDialog()
        }
    }
}
